//
//  NewfeatureViewController.swift
//  Weibo
//
//  Created by 李亚乐 on 15/12/9.
//  Copyright © 2015年 李亚乐. All rights reserved.
//

import UIKit
import SnapKit

private let WBNewFeatureViewCellID = "WBReuseIdentifier"
private let WBNewFeatureCount = 4

class NewfeatureViewController: UICollectionViewController {

    //MARK: - 重写构造函数
    init() {
        let layout = UICollectionViewFlowLayout()
        /// 设置 collectionFlowLayout
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.pagingEnabled = true
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
//MARK: - 视图加载完成
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView?.registerClass(NewfeatureCell.self, forCellWithReuseIdentifier: WBNewFeatureViewCellID)
        
    }
    
/// 取消状态栏
    override func prefersStatusBarHidden() -> Bool {
        return false
    }
    
// MARK: - UICollectionView 数据源方法
    //Item 数量
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return WBNewFeatureCount
    }
    
    // Cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBNewFeatureViewCellID, forIndexPath: indexPath) as! NewfeatureCell
        
        cell.imageIndex = indexPath.item
        return cell
    }
    
    /// 滚动结束代理方法
    /// 播放动画
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        if page != WBNewFeatureCount - 1 {
            return
        }
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewfeatureCell
        cell.startButton.hidden = false
        cell.showAnimation()
        
        
    }
    

  }

//MARK: - 自定义 Item
private class NewfeatureCell: UICollectionViewCell {
    /// 图片
    private var iconView: UIImageView = UIImageView()
    /// 开始体验按钮
    private var startButton: UIButton = UIButton(backgroundImageName: "new_feature_finish_button", title: "开始体验", titleColor:  UIColor.whiteColor())
    /// Item 记录
    var imageIndex = 0 {
        didSet {
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
/// 重写构造方法 创建 Cell
    override init(frame: CGRect) {
        super.init(frame: frame)
        SetupUI()
    }
    //必选
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
/// 设置 Cell 的 UI
    private func SetupUI() {
        //添加图片
        addSubview(iconView)
        //指定位置
        iconView.frame = bounds
        //添加按钮
        addSubview(startButton)
        //指定位置
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.7)
            startButton.addTarget(self, action: "clickStartButoon", forControlEvents: UIControlEvents.TouchUpInside)
        }
    }
    
    private func showAnimation() {
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)

        UIView.animateWithDuration(1.0,
            delay: 0,
            usingSpringWithDamping: 0.7, //弹力系数 0 - 1
            initialSpringVelocity: 15,  //初始速度
            options: [],
            animations: { () -> Void in
                self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
            self.startButton.userInteractionEnabled = true
                
        }
    }
    
    @objc private func clickStartButoon() {
        
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootControllerNotification, object: nil)
    }
}



